set more off

use dataset, clear

*** election year indicator  ***

gen electionyear = year == 1992 | year == 1996 | year == 2000 | year == 2004 | year == 2008 | year == 2012

*** set time series ***

xtset townid year
keep if year <= 2012 & year >= 1990

*** in text description ***

egen noverride = sum(override), by(town)
tab noverride if year == 2000

egen noverride_el = sum(override) if electionyear == 1, by(town)
tab noverride_el if year == 2000 

egen noverridep = sum(overridep), by(town)
tab noverridep if year == 2000

egen noverridep_el = sum(overridep) if electionyear == 1, by(town)
tab noverridep_el if year == 2000 

preserve
	replace numberpassed = 0 if mi(numberpassed)
	gen morethanonep = numberpassed > 1 & !mi(numberpassed)
	tab year morethanonep, row nofreq
	su morethanonep
	
	replace numberheld = 0 if mi(numberheld)
	gen morethanone = numberheld > 1 & !mi(numberheld)
	tab year morethanone, row nofreq
	su morethanone
restore

replace override = 1 if numberheld > 0 & !mi(numberheld)
replace overridep = 1 if numberpassed > 0 & !mi(numberpassed)

*** table ***

keep if !mi(overridep) & !mi(overridel)

egen num = sum(overridep), by(year)
egen den = sum(override), by(year)
gen passage = round(num/den*100)

collapse (sum) overridep overridel override (mean) passage, by(year electionyear)
reg override electionyear
reg overridep electionyear
tsset year
	
#delimit;
listtex 
	year override overridep passage using temp.tex
	,
	type rstyle(tabular)
	head(
		"\begin{tabular}{cccc}\toprule""Affected year
		&Holding referendum&Passing referendum&Passage rate\\
		
		&(N)&(N)&(\%)\\
		\midrule"
	)
	foot("\bottomrule\end{tabular}");
#delimit cr
shell mv "temp.tex" "table 1.tex"
shell "./shader.sh"  "table 1.tex"
